<template>
  <div id="scroll-board">
    <dv-scroll-board :config="config" />
  </div>
</template>

<script>
  import {  getView,listAchievement2} from "@/api/module/cjl/achievement";

  export default {
  name: 'ScrollBoard',
    inject:['reload'],
  data () {
    return {
      user: '',
      reload1: 0,
      // 以下websocket
      wsIsRun: false,
      // 定义ws对象
      webSocket: null,
      // ws连接次数
      wsNumber: 0,
      // ws请求链接（类似于ws后台地址）
      ws: '',
      // ws定时器
      wsTimer: null,
      // 以上websocket
      unreadMsgNums: 0,
      unreadMsgNumsShow: '',
      hasConnected: false,
      datr:null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        resultName: null,
        resultType: null,
        resultIntroduction: null,
        innovationPoint: null,
        competitionExperience: null,
        picturePath1: null,
        picturePath2: null,
        resultCategory: null,
        resultGrade: null,
        resultSource: null,
        resultBackground: null,
        resultMeaning: null,
        projectTime: null,
        technology: null,
        completionUnit: null,
        applyName: null,
        applyNumber: null,
        applyTime: null,
        auditState: null,
        auditTime: null,
        auditName: null,
        auditNumber: null,
        isShow: null,
        firstName: null,
        firstGrade: null,
        firstMajor: null,
        firstNumber: null,
        firstCollege: null,
        firstPhone: null,
        firstIdnumber: null,
        secondName: null,
        secondGrade: null,
        secondMajor: null,
        secondNumber: null,
        secondCollege: null,
        secondPhone: null,
        secondIdnumber: null,
        thirdName: null,
        thirdGrade: null,
        thirdMajor: null,
        thirdNumber: null,
        thirdCollege: null,
        thirdPhone: null,
        thirdIdnumber: null,
        fourthName: null,
        fourthGrade: null,
        fourthMajor: null,
        fourthNumber: null,
        fourthCollege: null,
        fourthPhone: null,
        fourthIdnumber: null,
        fifthName: null,
        fifthGrade: null,
        fifthMajor: null,
        fifthNumber: null,
        fifthCollege: null,
        fifthPhone: null,
        fifthIdnumber: null,
        advisorName1: null,
        advisorTitl1: null,
        advisorMajor1: null,
        advisorNumber1: null,
        advisorName2: null,
        advisorTitl2: null,
        advisorMajor2: null,
        advisorNumber2: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null
      },
      config: {
        header: ['时间', '成果名称', '学院', '是否转化'],
        data:[],
        index: true,
        columnWidth: [50, 170, 300],
        align: ['center'],
        rowNum: 7,
        headerBGC: '#1981f6',
        headerHeight: 45,
        oddRowBGC: 'rgba(0, 44, 81, 0.8)',
        evenRowBGC: 'rgba(10, 29, 50, 0.8)'
      }
    }
  },
    created() {
      this.wsIsRun = true
      this.wsNumber = 1
      this.wsInit();
      this.$watch(
        () => [this.$route.query.userId],
        () => {
          if(this.reload1 < 1){
            // alert(1);
            this.reload1++ ;
            const userId = this.$route.query.userId;
            this.$router.push({path: "/view1",query: {userId}})
          }

        }
      );
    },
    mounted() {
      this.getlistdata();

    },
    methods: {
      /**
       * 初始化ws
       */
      wsInit() {
        // console.log("userName: " + this.$store.state.user.ipp)
        console.log("user: " + this.$store.state.constip.ipp)
        // const wsuri = 'ws://' + '210.47.178.218' + ':8080/library/wsService/' + this.$store.state.user.name
        this.getConfigKey("sys.account.ip").then(response => {
          this.sysipp = response.msg;
        });
        console.log("sysipp: " + this.sysipp)
        const wsuri = 'ws://' + this.sysipp + '/library/wsService/' +
          this.$store.state.constip.ipp + "_" + Math.floor(Math.random() * (99999 - 10000) + 10000);
        console.log("wsuri: " + wsuri)
        this.ws = wsuri
        if (!this.wsIsRun)
          return
        // 销毁ws
        this.wsDestroy()
        // 初始化ws
        this.webSocket = new WebSocket(this.ws)
        // ws连接建立时触发

        this.webSocket.addEventListener('open', this.wsOpenHanler)
        // ws服务端给客户端推送消息

        this.webSocket.addEventListener('message', this.wsMessageHanler)

        // ws通信发生错误时触发
        this.webSocket.addEventListener('error', this.wsErrorHanler)
        // ws关闭时触发

        this.webSocket.addEventListener('close', this.wsCloseHanler)

        clearInterval(this.wsTimer);
      },
      subSend(userId) {

        //获取msg的value
        // var msg = document.getElementById("msg").value;

        this.webSocket.send(userId);

        // ws服务端给客户端推送消息

        // document.getElementById("msg").value="";
      },


      wsOpenHanler(event) {
        console.log('ws建立连接成功')
      },
      wsMessageHanler(e) {

        //Cookies.set('unreadNums', '6')

        this.openMsg(e)
        //const redata = JSON.parse(e.data)
        //console.log(redata)

      },
      /**
       * ws通信发生错误
       */
      wsErrorHanler(event) {
        console.log(event, '通信发生错误')
        console.log("第" + this.wsNumber + "次断线重连(5次未连接将自动断开)")
        this.wsNumber++
        if (this.wsNumber < 6) this.wsInit();
        //this.wsInit()
      },
      /**
       * ws关闭
       */
      wsCloseHanler(event) {
        console.log(event, 'ws关闭')
        console.log("websocket连接超时，已自动断开")
        //this.wsInit()
      },
      /**
       * 销毁ws
       */
      wsDestroy() {
        console.log("hello world")
        if (this.webSocket !== null) {
          this.webSocket.removeEventListener('open', this.wsOpenHanler)
          this.webSocket.removeEventListener('message', this.wsMessageHanler)
          this.webSocket.removeEventListener('error', this.wsErrorHanler)
          this.webSocket.removeEventListener('close', this.wsCloseHanler)
          this.webSocket.close()
          this.webSocket = null
          clearInterval(this.wsTimer)
          console.log("zoul111")
        } else {
          console.log("zoul")
        }
      },
      /**
       * 弹窗
       */
      openMsg(e) {
        const h = this.$createElement;
        if (e.data) {
          if(e.data==1) {
            this.$router.push({path: "/dashujuzhanshi/dashujushouye"})
          }
          else if(e.data==2){
            this.$router.push({path: "/shou"})
          }else if(e.data==3){
            this.$router.push({path: "/mwnhugongshi"})
          }else if(e.data==4){
            this.$router.push({path: "/liucheng"})
          }else if(e.data==5){
            this.$router.push({path: "/dashujuzhanshi/xueyuan"})
          }else if(e.data==6){
            this.$router.push({path: "/dashujuzhanshi/nianfen"})
          }else if(e.data==10){
            window.scrollBy(0, 100);
          }else if(e.data==11){
            window.scrollBy(0, -100);
          }else if(e.data==12){
            this.queryParams.signingStatus="0";

          }else if(e.data==21){
            this.$router.push({path: "/ykqlogin3"})
          } else if(e.data==22){
            this.$router.push({path: "/ykqlogin4"})
          }else if(e.data==23){
            this.$router.push({path: "/ykqlogin5"})
          }else if(e.data==24){
            this.$router.push({path: "/shou"})
          }else if(e.data==210){
            this.$router.push({path: "/index"})
          }else if(e.data==211){
            this.$router.push({path: "/transformation"})
          }else if(e.data==212){
            this.$router.push({path: "/wodehetong"})
          }else if(e.data==213){
            this.$router.push({path: "/expense"})
          }else if(e.data==214){
            this.$router.push({path: "/qiye"})
          }else if(e.data==215){
            this.$router.push({path: "/geren"})
          }else if(e.data==216){
            this.$router.push({path: "/chengguogongshi"})
          }else if(e.data==217){
            this.$router.push({path: "/daochuxinxi"})
          }else if(e.data==218){
          this.$router.push({path: "/resultapply"})
        }else if(e.data==13){
            this.queryParams.signingStatus="1";

          }else if(e.data>500&&e.data<2000){
            let id=e.data-500
            this.$router.push({path: "/detail",query: {id}})

          }else if(e.data>2000&&e.data<3000){
            let nianfen=e.data
            this.$router.push({path: "/dashujuzhanshi/xueyuan",query: {nianfen}})

          }else if(e.data>3000&&e.data<4000){
            let firstcollege=e.data
            let i_xueyuan=firstcollege-3000
            let xueyuan = '';
            if(i_xueyuan<10){
              xueyuan='00'+i_xueyuan;
            }else if(i_xueyuan>=10 && i_xueyuan<100){
              xueyuan='0'+i_xueyuan;
            }else{

            }
            this.$router.push({path: "/dashujuzhanshi/nianfen",query: {xueyuan}})


          }
          else{
              let userId=e.data
              this.$router.push({path: "/view",query: {userId}})

          }
          /*var dv = document.getElementById("dv");
          dv.innerHTML += e.data + " ";*/
          /*this.$router.push({path: "/"});*/

          var data = JSON.parse(e.data)
          var route = this.$router;
          var msgType = data.msgType;
          /*if (msgType == "push") {
            // 如果是推送的是一条消息
            this.unreadMsgNums++;
            if (this.unreadMsgNums > 99) {
              this.unreadMsgNumsShow = "99+";
            } else {
              this.unreadMsgNumsShow = String(this.unreadMsgNums);
            }

            this.$notify.info({
              title: data.msgTitle,
              message: data.msgContent,
              onClick() {
                console.log(route)
                route.push({path: "/library/yyg/msg/list"});
              }
            });
          } else if (msgType == "unread") {
            // 如果推送的是未读消息数
            //Cookies.set('unreadNums', data.unreadNums);
            this.unreadMsgNums = Number(data.unreadNums);
            if (this.unreadMsgNums > 0) {
              this.unreadMsgNumsShow = String(this.unreadMsgNums);
              if (this.unreadMsgNums > 99) {
                this.unreadMsgNumsShow = "99+";
              }
              this.$notify.info({
                title: "消息提醒",
                //message: data.msgContent,
                message: "您当前有" + this.unreadMsgNumsShow + "条未读消息，点击此处查看",
                onClick() {
                  console.log(route)
                  route.push({path: "/library/yyg/msg/list"});
                }
              });
            }
          } else if (msgType == "read") {
            // 如果推送的是用户已读消息
            this.unreadMsgNums--;
            if (this.unreadMsgNums > 0 && this.unreadMsgNums <= 99) {
              this.unreadMsgNumsShow = String(this.unreadMsgNums);
            } else if (this.unreadMsgNums <= 0) {
              this.unreadMsgNumsShow = '';
            }
          }
*/
        }
      },

getlistdata(){
  let a =  this.$route.query.userId;
  listAchievement2(a).then((response) => {
    this.config.data = response;

    console.log(    this.config.data );
    this.config = {header: ['成果名称','完成单位','成果类型','成果类别','成果等级','成果来源','成果背景','技术领域','立项时间'],
      data: this.config.data,
      index: true,
      columnWidth: [90, 200, 180,250, 200,200, 200,200, 200,200],
      align: ['center'],
      rowNum: 9,
      headerBGC: '#1981f6',
      headerHeight: 45,

      oddRowBGC: 'rgba(0, 44, 81, 0.8)',
      evenRowBGC: 'rgba(10, 29, 50, 0.8)'}


 /*   console.log(this.config.data);*/
  })
},




    }
}
</script>

<style lang="less" scoped>
  #scroll-board {
    width: 93%;
    box-sizing: border-box;
    height: 80%;
    overflow: hidden;
    margin: 0 auto;
  }

</style>
